package com.zareal.ymcc.web.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zareal.ymcc.domain.Course;
import com.zareal.ymcc.dto.CourseDto;
import com.zareal.ymcc.query.CourseQuery;
import com.zareal.ymcc.result.JSONResult;
import com.zareal.ymcc.result.PageList;
import com.zareal.ymcc.service.ICourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;

    /**
     * 新增课程
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JSONResult save(@RequestBody @Valid CourseDto courseDto) {
        if (courseDto.getCourse().getId() == null) {
            courseService.save(courseDto);
        }else {
            courseService.update(courseDto);
        }
        return JSONResult.success();
    }
    /**
     * 批量上架
     */
    @RequestMapping(value = "/patchOnLineCourse", method = RequestMethod.PATCH)
    public JSONResult patchOnLineCourse(@RequestBody List<Long> ids) {
        courseService.patchOnLineCourse(ids);
        return JSONResult.success();
    }
    /**
     * 批量下架
     */
    @RequestMapping(value = "/patchOffLineCourse", method = RequestMethod.PATCH)
    public JSONResult patchOffLineCourse(@RequestBody List<Long> ids) {
        courseService.patchOffLineCourse(ids);
        return JSONResult.success();
    }
    /**
     * 课程发布
     */
    @RequestMapping(value = "/onLineCourse/{id}", method = RequestMethod.POST)
    public JSONResult onLineCourse(@PathVariable("id") Long courseId) {
            courseService.onLineCourse(courseId);
        return JSONResult.success();
    }
    /**
     * 课程下架
     */
    @RequestMapping(value = "/offLineCourse/{id}", method = RequestMethod.POST)
    public JSONResult offLineCourse(@PathVariable("id") Long courseId) {
        courseService.offLineCourse(courseId);
        return JSONResult.success();
    }
    /**
     * 删除对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id) {
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id") Long id) {
        return JSONResult.success(courseService.selectById(id));
    }


    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JSONResult list() {
        return JSONResult.success(courseService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseQuery query) {
        Page<Course> page = new Page<Course>(query.getPage(), query.getRows());
        Wrapper<Course> wrapper = new EntityWrapper<>();
        wrapper.like("name", query.getKeyword());
        page = courseService.selectPage(page, wrapper);
        return JSONResult.success(new PageList<Course>(page.getTotal(), page.getRecords()));
    }

    /**
     * 根据课程Id查询课程详情
     * @param courseId
     * @return
     */
    @RequestMapping(value = "/detail/data/{courseId}",method = RequestMethod.GET)
    public JSONResult getDetail(@PathVariable("courseId")Long courseId){
        return JSONResult.success(JSON.toJSON(courseService.getDetail(courseId)));
    }

    /**
     * 支付页面获取信息，根据课程id
     */
    @RequestMapping(value = "/info/{courseIds}",method = RequestMethod.GET)
    public JSONResult getInfo(@PathVariable("courseIds")String courseIds){
        return JSONResult.success(courseService.getInfo(courseIds));
    }
}
